package com.zmn.plat.services.impl.appointment.appointmentsetting;

import java.util.Iterator;
import java.util.List;
import java.util.Objects;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.zmn.manager.common.database.annotation.ReadOnlyConnection;
import com.zmn.plat.model.entity.appointment.appointmentsetting.AppointmentSetting;
import com.zmn.plat.model.entity.appointment.appointmentsetting.AppointmentSettingQuery;
import com.zmn.plat.persistence.interfaces.appointment.appointmentsetting.AppointmentSettingDAO;
import com.zmn.plat.services.interfaces.appointment.appointmentsetting.AppointmentSettingService;

import lombok.extern.slf4j.Slf4j;

/**
 * 描述: 预约设置基础接口实现类
 *
 * @author xujie
 * @since 2020-09-04 16:29:25
 **/
@Slf4j
@Service
public class AppointmentSettingServiceImpl implements AppointmentSettingService {

    private static final String TAG = "预约设置基础服务接口";

    @Resource
    private AppointmentSettingDAO appointmentSettingDao;

    /**
     * 描述: 根据id查询数据
     *
     * @param id
     *            id
     * @author xujie
     * @since 2020-09-04 16:29:25
     **/
    @Override
    @ReadOnlyConnection
    public AppointmentSetting findById(Integer id) {

        if (Objects.isNull(id)) {
            log.error("[{}], 根据id查询数据id为null", TAG);
            return null;
        }
        return appointmentSettingDao.findById(id);
    }

    /**
     * 描述: 按条件查询数据 支持分页
     *
     * @param query 查询对象
     * @author xujie
     * @since 2020-09-04 16:29:25
     **/
    @Override
    @ReadOnlyConnection
    public List<AppointmentSetting> listPageByQuery(AppointmentSettingQuery query) {

        if (Objects.isNull(query)) {
            log.error("[{}], 按条件查询数据, query为null", TAG);
            return null;
        }
        int count = this.countByQuery(query);
        query.setTotalCount(count);
        return appointmentSettingDao.listPageByQuery(query);
    }

    /**
     * 描述: 按条件查询数据 不支持分页
     *
     * @param query
     *            查询对象
     * @author xujie
     * @since 2020-09-04 16:29:25
     **/
    @Override
    @ReadOnlyConnection
    public List<AppointmentSetting> listByQuery(AppointmentSettingQuery query) {

        if (Objects.isNull(query)) {
            log.error("[{}],按条件查询数据,query为null", TAG);
            return null;
        }
        return appointmentSettingDao.listByQuery(query);
    }

    /**
     * 描述: 按条件查询数据 不支持分页
     *
     * @param query 查询对象
     * @author xujie
     * @since 2020-09-04 16:29:25
     **/
    @Override
    @ReadOnlyConnection
    public Integer countByQuery(AppointmentSettingQuery query) {

        if (Objects.isNull(query)) {
            log.error("[{}], 按条件统计数据, query为null", TAG);
            return null;
        }
        return appointmentSettingDao.countByQuery(query);
    }

    /**
     * 描述: 根据id修改数据 id为必传
     *
     * @param appointmentSetting 待修改数据
     * @return java.lang.Integer
     * @author xujie
     * @since 2020-09-04 16:29:25
     **/
    @Override
    public Integer updateByKey(AppointmentSetting appointmentSetting) {

        if (Objects.isNull(appointmentSetting.getId())) {
            log.error("[{}], 根据id修改数据, appointmentSetting为null", TAG);
            return null;
        }
        return appointmentSettingDao.updateByKey(appointmentSetting);
    }

    /**
     * 描述: 根据id批量修改数据 id为必传
     *
     * @param appointmentSettingList 待修改数据集合
     * @return java.lang.Integer 成功个数
     * @author xujie
     * @since 2020-09-04 16:29:25
     **/
    @Override
    public Integer updateBatchByKey(List<AppointmentSetting> appointmentSettingList) {

        if (appointmentSettingList.size() == 0) {
            log.error("[{}], 根据id批量修改数据, appointmentSettingList为空", TAG);
            return null;
        }
        Iterator<AppointmentSetting> it = appointmentSettingList.iterator();
        while (it.hasNext()) {
            AppointmentSetting appointmentSetting = it.next();
            if (Objects.isNull(appointmentSetting.getId())) {
                log.error("[{}], 根据id批量修改数据, appointmentSetting为null已跳过该条数据", TAG);
                it.remove();
            }
        }
        if (appointmentSettingList.size() == 0) {
            log.error("[{}], 根据id批量修改数据, appointmentSettingList长度为0", TAG);
            return null;
        }
        return appointmentSettingDao.updateBatchByKey(appointmentSettingList);
    }

    /**
     * 描述: 新增数据
     *
     * @param appointmentSetting 待新增数据
     * @return java.lang.Integer
     * @author xujie
     * @since 2020-09-04 16:29:25
     **/
    @Override
    public Integer insert(AppointmentSetting appointmentSetting) {

        if (Objects.isNull(appointmentSetting)) {
            log.error("[{}],新增数据,appointmentSetting为null", TAG);
            return null;
        }
        return appointmentSettingDao.insert(appointmentSetting);
    }

    /**
     * 描述: 批量新增数据
     * 
     * @param appointmentSettingList 待新增数据集合
     * @return java.lang.Integer 成功个数
     * @author xujie
     * @since 2020-09-04 16:29:25
     */
    @Override
    public Integer insertBatch(List<AppointmentSetting> appointmentSettingList) {

        if (appointmentSettingList.size() == 0) {
            log.error("[{}], 批量新增数据, appointmentSettingList为空", TAG);
            return null;
        }
        Iterator<AppointmentSetting> it = appointmentSettingList.iterator();
        while (it.hasNext()) {
            AppointmentSetting appointmentSetting = it.next();
            if (Objects.isNull(appointmentSetting)) {
                log.error("[{}], 批量新增数据, appointmentSetting为null已跳过该条数据", TAG);
                it.remove();
            }
        }
        if (appointmentSettingList.size() == 0) {
            log.error("[{}], 批量新增数据, appointmentSettingList长度为0", TAG);
            return null;
        }
        return appointmentSettingDao.insertBatch(appointmentSettingList);
    }

    /**
     * 描述: 根据主键删除数据
     *
     * @param id 主键
     * @return: 执行结果码
     * @auther: xujie
     * @since: 2020-09-04 16:29:25
     */
    @Override
    public Integer deleteByKey(Integer id) {

        if (Objects.isNull(id)) {
            log.error("[{}], 根据id删除数据, id为null", TAG);
            return null;
        }
        return appointmentSettingDao.deleteByKey(id);
    }

}